package com.clkj.goldpartner.pojo.common;


import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * @author 杨行
 * @date 2020/12/19 16:52
 */
public class MessageCode {

    static String URL = "http://api02.monyun.cn:7901/sms/v2/std/single_send";
    static String URL_group = "http://api01.monyun.cn:7901/sms/v2/std/batch_send";

    public static String getmsg(String account, String context) {

        String encode = null;
        String aa = null;
        try {
            encode = URLEncoder.encode(context, "GBK");
            JSONObject jsobj1 = new JSONObject();
            jsobj1.put("apikey", "ab014062730430ed3c93af3f3007dae8");
            jsobj1.put("mobile", account);
            jsobj1.put("content", encode);
            aa = post(jsobj1);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return aa;
    }

    public static String post(JSONObject json) {
        HttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(URL);
        post.setHeader("Content-Type", "application/json");
        post.addHeader("Authorization", "Basic YWRtaW46");
        String result = "";
        try {
            StringEntity s = new StringEntity(json.toString(), "utf-8");
            s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                    "application/json"));
            post.setEntity(s);
            HttpResponse httpResponse = client.execute(post);
            InputStream inStream = httpResponse.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    inStream, "utf-8"));
            StringBuilder strber = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                strber.append(line + "\n");
            }
            inStream.close();
            result = strber.toString();
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            } else {
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }
}
